﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UtilityExtensions;
using DAL;
using BOL;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Data.Linq;
using System.Drawing;
using Website.Logger;


namespace UILGestion.ObraAdmin.Presupuesto
{
    public partial class PartidaGrid : System.Web.UI.UserControl
    {
        public PartidaGrid()
        {
            bol = new PresupuestoBL();
        }

        private PresupuestoBL bol;

        public GridView grupoItem
        {
            get
            {
                return this.itemGridView;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                // inicializamos el esquema


                
             
                FormatForm();
            }
        }

        private void FormatForm()
        {
            // ShowComboBox();

           // BindDataItem();
        }
        

        private void setFooterRow()
        {
            //DropDownList proyectoTipoList = ((DropDownList)itemGridView.FooterRow.FindControl("proyectoTipoDdl"));

            //proyectoTipoList.DataValueField = "idInternal";
            //proyectoTipoList.DataTextField = "descripcion";
            //proyectoTipoList.DataSource = DbUtil.Db.ProyectoTipos.ToList();
            //proyectoTipoList.DataBind();
        }



        protected void UpdateItem(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                GridView gv = ((GridView)sender);
                string descripcion =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("partidaTxt")).Text;

                string codigoTxt =
                    ((TextBox)gv.Rows[e.RowIndex].FindControl("codigoTxt")).Text;


                int idinternal = int.Parse(gv.DataKeys[e.RowIndex].Values[0].ToString());
                dbGestionDataContext _db = new dbGestionDataContext(Parametros.strConnString);
                bol.UpdatePartida(_db, idinternal, codigoTxt, descripcion);
                    

                gv.EditIndex = -1;
                itemGridView.FooterRow.Visible = true;

                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }


        public void Load(string id, int idestado,TipoObra tipoObra)
        {
            this.idParent.Value = id;
            this.idEstado.Value = idestado.ToString();
            this.bitViewPresupuestoEditEver.Value = tipoObra.bitViewPresupuestoEditEver.ToInt().ToString();
            BindDataItem();

        }
        protected void EditItem(object sender, GridViewEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = e.NewEditIndex;
                BindDataItem();
                itemGridView.FooterRow.Visible = false;
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        public void MonstrarDetalle(int rowindex, string idPadre)
        {
            string div = "div" + idPadre.ToString();
            string script = String.Empty;

            ImageButton lnkdivrecurso = (ImageButton)itemGridView.Rows[rowindex].FindControl("imgdiv");
            Panel divcol = (Panel)itemGridView.Rows[rowindex].FindControl("divgrillan1");

            RecursoGrid gv = (RecursoGrid)itemGridView.Rows[rowindex].FindControl("RecursoGrid1");
            

            UpdatePanel panel = (UpdatePanel)itemGridView.Rows[rowindex].FindControl("UpdateDetalleUpdatePanel");


            if (lnkdivrecurso.ImageUrl == "../../Images/expand_button_down.png")
            {
                lnkdivrecurso.ImageUrl = "../../Images/expand_button.png";
                divcol.Style.Add("display", "none");
            
            }
            else
            {

                gv.Load(idPadre, idEstado.Value.ToInt(),bitViewPresupuestoEditEver.Value.ToBool());
                    divcol.Style.Add("display", "inline");

                    lnkdivrecurso.ImageUrl = "../../Images/expand_button_down.png";

            }
            panel.Update();
        }

        protected void MonstrarDetalle(object sender, EventArgs e)
        {
            try
            {
                ImageButton lnkdivrecurso = (ImageButton)sender;
                string idPadre = lnkdivrecurso.CommandName.ToString();
                int rowindex = int.Parse(lnkdivrecurso.CommandArgument);


                MonstrarDetalle(rowindex, idPadre);


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
       
        protected void CancelItem(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                itemGridView.EditIndex = -1;
                itemGridView.FooterRow.Visible = true;
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }
        protected void AddItem(object sender, EventArgs e)
        {
            try
            {


                TextBox partidaTxt =
                    ((TextBox)itemGridView.FooterRow.FindControl("partidaTxt"));


                bol.AddPartida(new dbGestionDataContext(Parametros.strConnString), 
                                        partidaTxt.Text,
                                        idParent.Value.ToInt());
               
                BindDataItem();
                 //GridUpdatePanel.Update();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }



        protected void DeleteItem(object sender, EventArgs e)
        {
            try
            {

                ImageButton lnkRemove = (ImageButton)sender;
                int idInternal = int.Parse(lnkRemove.CommandArgument.ToString());

                var partida = bol.deletePartida(idInternal);
                
                
                BindDataItem();


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "").Replace("\r", "").Replace("\n", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }


        private void resetEmpty()
        {
            if (itemGridView.Rows.Count == 0)
            {
                List<PartidadEditInfo> itemList0 = new List<PartidadEditInfo>();
                PartidadEditInfo itemcero = new PartidadEditInfo();

                itemList0.Add(itemcero);
                itemGridView.DataSource = itemList0;
                itemGridView.DataBind();
                itemGridView.Rows[0].Visible = false;
            }

        }

        public void BindDataItem()
        {


            itemGridView.DataSource = bol.getListPartida(idParent.Value.ToInt());

            if (idEstado.Value.ToInt() == 26 & (this.bitViewPresupuestoEditEver.Value=="0")) //acabado// or promosion
            {
                itemGridView.ShowFooter = false;
                itemGridView.Columns[6].Visible = false;
                itemGridView.Columns[7].Visible = false;
                itemGridView.DataBind();
            }
            else
            {
                itemGridView.Columns[6].Visible = true;
                itemGridView.Columns[7].Visible = true;
                itemGridView.DataBind();
                resetEmpty();
                setFooterRow();
            }


        }


        protected void itemRowDataBound(Object sender, GridViewRowEventArgs e)
        {
            try
            {



                if (e.Row.RowType == DataControlRowType.DataRow)
                {




                }


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this.Page, typeof(UserControl),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }


    }
}